import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class DeviceToolsService {

  constructor(private http:HttpClient) { }
  private Fixture = '/Fixture';
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数

  CreateFixtureType(data:any){
    const url = this.Fixture+'/CreateFixtureType';
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };
    return this.http.post(environment.apiUrl+ url, data, httpOptions);
  } // 创建工装夹具类型

  DeleteFixtureType(id:number){
    const url = this.Fixture+'/DeleteFixtureType';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }// 删除工装夹具类型

  UpdateFixtureType(data:any, id:number){
    const url = this.Fixture+'/UpdateFixtureType';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改工装夹具类型

  GetFixtureTypes(){
    return this.http.get(environment.apiUrl+ this.Fixture+'/GetFixtureTypes');
  }// 获取所有工装夹具类型

  GetPagedFixtureTypesWithChildren(page?: number, pageSize?: number){
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = this.createPaginationParams(page, pageSize);
    }
    return this.http.get(environment.apiUrl+ this.Fixture+'/GetPagedFixtureTypesWithChildren',{
      params: params
    })
  }// 获取所有工装夹具类型
  GetFixtureTypeById(id:number){
    return this.http.get(environment.apiUrl+ this.Fixture+'/GetFixtureTypeById',{
      params:{id}
    })
  }// 获取工装夹具类型详情

  GetFixtures(FixtureTypeId?:number,Model?:string,SearchTerm?:string,PageNumber?:number,PageSize?:number){
    let params = new HttpParams();
    if (FixtureTypeId ) {
      params = params.set('FixtureTypeId', FixtureTypeId.toString());
    }
    if (Model ) {
      params = params.set('Model', Model);
    }
    if (SearchTerm ) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (PageNumber) {
      params = params.set('PageNumber', PageNumber.toString());
    }
    if (PageSize) {
      params = params.set('PageSize', PageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.Fixture+'/GetFixtures',{
      params: params
    })
  }// 获取所有工装夹具
  UploadAttachment(file:File){
    const url = this.Fixture+'/UploadAttachment';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }// 上传附件

  CreateFixture(data:any){
    const url = this.Fixture+'/CreateFixture';
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };
    return this.http.post(environment.apiUrl+ url, data, httpOptions);
  }// 创建工装夹具

  GetFixtureById(id:number){
    return this.http.get(environment.apiUrl+ this.Fixture+'/GetFixtureById',{
      params:{id}
    })
  }// 获取工装夹具详情

  DeleteFixtures(ids:any){
    const url = this.Fixture+'/DeleteFixtures';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }// 批量删除工装夹具

  UpdateFixture(data:any, id:number){
    const url = this.Fixture+'/UpdateFixture';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改工装夹具
}
